package com.mock.ems.controller.distribution;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.mock.base.util.CollectionUtil;
import com.mock.base.util.DateUtil;
import com.mock.base.util.StringUtil;

import java.util.*;

public class DistributionAreaAgentStatController extends Controller{

    public void index() {
        renderJsp("/page/distribution/distribution_area_agent.jsp");
    }

    public void pagination() {

        int pageNumber = getParaToInt("pageNumber");
        int pageSize = getParaToInt("pageSize");

        String selectSQL = "SELECT user_id,DATE_FORMAT(create_time,'%Y-%m-%d') as settleTime,sum(income) as totalIncome";
        StringBuilder exceptSelectSQL =
                new StringBuilder(" from distribution_area_agent_detail where `status` = 1");

        List<Object> params = new ArrayList<Object>();

        String phone = getPara("phone");
        if (StringUtil.isNotEmpty(phone)) {
            List<Record> userList = Db.find("select id from user where phone like ?",'%' + phone + '%');
            if (CollectionUtil.isNotEmpty(userList)) {
                exceptSelectSQL.append(" and user_id in ( ");
                userList.forEach(item -> {
                    exceptSelectSQL.append("?,");
                    params.add(item.getLong("id"));
                });
                exceptSelectSQL.deleteCharAt(exceptSelectSQL.length() - 1);
                exceptSelectSQL.append(") ");
            }
        }

        Date begin_time = DateUtil.parseToDate(getPara("begin_time"),DateUtil.SIMPLE_PATTERN);
        Date end_time = DateUtil.parseToDate(getPara("end_time"),DateUtil.SIMPLE_PATTERN);

        if (null != begin_time && end_time == null) {
            exceptSelectSQL.append(" and DATE_FORMAT(create_time,'%Y-%m-%d') >= ? ");
            params.add(begin_time);
        }

        if (null == begin_time && end_time != null) {
            exceptSelectSQL.append(" and DATE_FORMAT(create_time,'%Y-%m-%d')<= ? ");
            params.add(end_time);
        }

        if (null != begin_time && null != end_time) {
            exceptSelectSQL.append(" and DATE_FORMAT(create_time,'%Y-%m-%d') >= ? and DATE_FORMAT(create_time,'%Y-%m-%d') <= ? ");
            params.add(begin_time);
            params.add(end_time);
        }

        exceptSelectSQL.append(" GROUP BY user_id,DATE_FORMAT(create_time,'%Y-%m-%d') ");
        exceptSelectSQL.append(" ORDER BY DATE_FORMAT(create_time,'%Y-%m-%d') asc ");

        Page<Record> recordList = null;
        if (params.size() == 0) {
            recordList = Db.paginate(pageNumber, pageSize, selectSQL, exceptSelectSQL.toString());
        }else {
            recordList = Db.paginate(pageNumber, pageSize, selectSQL, exceptSelectSQL.toString(),params.toArray());
        }

        if (CollectionUtil.isNotEmpty(recordList.getList())) {
            recordList.getList().forEach(item -> {
                Long user_id = item.getLong("user_id");
                Record user = Db.findById("user",user_id);
                if (null != user) {
                    item.set("nick_name",user.getStr("nick_name"));
                    item.set("phone",user.getStr("phone"));
                }else{
                    item.set("nick_name","用户已删除，userId:"+item.get("party_id_"));
                    item.set("phone","用户已删除，userId:"+item.get("party_id_"));
                }
            });
        }

        Map<String, Object> returnMap = new HashMap<String, Object>();
        returnMap.put("total", recordList.getTotalRow());
        returnMap.put("rows", recordList.getList());
        renderJson(returnMap);
    }

}
